package com.cuz.for2024;

import java.util.ArrayList;
import java.util.Arrays;

public class T27 {
    public static void main(String[] args) {
        int[] ints = {3, 2, 2, 3};
        T27 t27 = new T27();

        int i = t27.removeElement(ints, 3);
        System.out.println(i);
        System.out.println(Arrays.toString(ints));

    }
    public int removeElement(int[] nums, int val) {
        int i = 0;
        for (; i < nums.length; i++) {
            if (nums[i] == val) {
                int next = findNext(nums, val, i);
                if (next == -1) {
                    return i - 1;
                }
                swap(nums, i, next);
            }
        }
        return i;
    }

    private void swap(int[] nums, int left, int right) {
        int temp = nums[left];
        nums[left] = nums[right];
        nums[right] = temp;
    }

    private int findNext(int[] nums, int val, int start) {
        while (start < nums.length) {
            if (nums[start] != val) {
                return start;
            }
            start++;
        }

        return -1;
    }
}
